package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.purchase.dao.PurchaseSignForDao;
import com.wing.purchase.dao.PurchaseSignForProductDao;
import com.wing.purchase.model.entity.PurchaseSignForProduct;
import com.wing.purchase.model.request.PurchaseSignForProductForm;
import com.wing.purchase.model.response.ProductionTheWorkOrderVO;
import com.wing.purchase.model.response.PurchaseSignForProductVO;
import com.wing.purchase.util.PurchaseSignUtil;
import com.wing.web.db.config.util.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 采购签收产品-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchaseSignForProductServiceImpl extends ServiceImpl<PurchaseSignForProductDao, PurchaseSignForProduct> implements PurchaseSignForProductService {

    @Autowired
    private PurchaseSignForDao purchaseSignForDao;

    @Override
    public int add(PurchaseSignForProductForm purchaseSignForProductForm) {

        //校验
        PurchaseSignForProduct purchaseSignForProduct = new PurchaseSignForProduct();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(PurchaseSignForProduct.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseSignForProductForm, purchaseSignForProduct, copyOptions);
        return baseMapper.insert(purchaseSignForProduct);
    }

    @Override
    public List<PurchaseSignForProductVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchaseSignForProductVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchaseSignForProductVO> list = baseMapper.list4app(page, params);
        //生产入库-开工单列表
        Map<String, Object> productionWorkOrderMap = new HashMap<>();
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        productionWorkOrderMap.put("databaseUser", databaseUser);
        List<ProductionTheWorkOrderVO> productionWorkOrderList = purchaseSignForDao.productionWorkOrderList(productionWorkOrderMap);

        Map<Long, ProductionTheWorkOrderVO> workOrderMap = productionWorkOrderList.stream()
                .collect(Collectors.toMap(ProductionTheWorkOrderVO::getId, Function.identity()));

        for (PurchaseSignForProductVO purchaseSignForProduct : list) {
            if(purchaseSignForProduct.getInventoryTypeCode()!=null &&
                    purchaseSignForProduct.getInventoryTypeCode().equals(PurchaseSignUtil.SCR)){
/*
                ProductionTheWorkOrderVO productionTheWorkOrder = productionWorkOrderList.stream().filter(t->t.getId() == purchaseSignForProduct.getSignProductId()).findFirst().orElse(null);
*/
                ProductionTheWorkOrderVO productionTheWorkOrder = workOrderMap.get(purchaseSignForProduct.getSignProductId());
                if(productionTheWorkOrder!=null){
                    purchaseSignForProduct.setQuantity(new BigDecimal(productionTheWorkOrder.getWorkQuantity()));
                    purchaseSignForProduct.setSkuCode(productionTheWorkOrder.getSkuCode());
                    purchaseSignForProduct.setSkuName(productionTheWorkOrder.getSkuName());
                    purchaseSignForProduct.setSkuSpec(productionTheWorkOrder.getSkuSpec());
                    purchaseSignForProduct.setBaseUnit(productionTheWorkOrder.getBaseUnit());
                    purchaseSignForProduct.setBaseUnitName(productionTheWorkOrder.getBaseUnitName());
                    purchaseSignForProduct.setMainCalWay(productionTheWorkOrder.getMainCalWay());
                    purchaseSignForProduct.setMainCalWayName(productionTheWorkOrder.getMainCalWayName());
                    purchaseSignForProduct.setMainUnitId(productionTheWorkOrder.getMainUnitId());
                    purchaseSignForProduct.setMainUnitName(productionTheWorkOrder.getMainUnitName());
                    purchaseSignForProduct.setMainUnitRate(productionTheWorkOrder.getMainUnitRate());
                    purchaseSignForProduct.setSubCalWay(productionTheWorkOrder.getSubCalWay());
                    purchaseSignForProduct.setSubCalWayName(productionTheWorkOrder.getSubCalWayName());
                    purchaseSignForProduct.setSubUnitId(productionTheWorkOrder.getSubUnitId());
                    purchaseSignForProduct.setSubUnitName(productionTheWorkOrder.getSubUnitName());
                    purchaseSignForProduct.setSubUnitRate(productionTheWorkOrder.getSubUnitRate());
                    purchaseSignForProduct.setProductDetailId(productionTheWorkOrder.getProductDetailId());

                    purchaseSignForProduct.setPurchaseQuantity(null);
                    purchaseSignForProduct.setUnitName(null);
                    purchaseSignForProduct.setRate(null);
                    purchaseSignForProduct.setTaxPrice(null);
                    purchaseSignForProduct.setPrice(null);
                    purchaseSignForProduct.setTax(null);
                    purchaseSignForProduct.setTotalAmount(null);
                }

            }
        }

        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseSignForProductVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<PurchaseSignForProductVO> findByPurchaseWarehousingPlanId(Long purchaseWarehousingPlanId) {
        return baseMapper.findByPurchaseWarehousingPlanId(purchaseWarehousingPlanId);
    }

    @Override
    public List<PurchaseSignForProductVO> findById(Long purchaseSignForId) {
        return baseMapper.findById(purchaseSignForId);
    }
}
